import { uploadFile } from '@/services';
import {
  getUserTokenLocalStorage,
  removeLocalStorageTokenName,
  removeUserLocalStorage,
} from '@/utils/localStorageModel';
import { PlusOutlined } from '@ant-design/icons';
import { history, request } from '@umijs/max';
import type { BtnConfigTypes } from '@vh-admin/pro-components/lib/t-table-custom/types';
import { defineConfig } from '@vh-admin/pro-components';
import theme from '../config/theme';

const headerTitleConfigArr: BtnConfigTypes = {
  text: {
    text: 'text',
    icon: PlusOutlined,
    type: 'primary',
    style: {
      background: '#96592b',
      borderColor: '#96592b',
    },
  },
};

const tableAlertOptionRenderConfigArr: BtnConfigTypes = {
  text: {
    text: 'text',
    type: 'link',
    danger: true,
  },
};
const operationConfigRenderConfigArr: BtnConfigTypes = {
  text: {
    text: 'text',
    type: 'link',
  },
};

defineConfig({
  theme,
  request,
  uploadFile,
  httpHeadersHook: (headers: any) => {
    let token: string | null = getUserTokenLocalStorage();
    if (token) {
      token = token.substring(1, token.length - 1);
    }
    // 如果已登录过
    // if (token) {
    //   headers.token = token;
    // }
    // 如果已登录过
    if (token) {
      headers.common.Authorization = `Bearer ${token}`;
    }
    return { ...headers };
  },
  httpErrorHook: (status: any) => {
    if (status === 401) {
      removeUserLocalStorage();
      removeLocalStorageTokenName();
      history.push('/login');
      return { errorMessageShow: false };
    } else if (status === 403) {
      // history.push('/403');
    } else if (status === 503) {
      history.push('/503');
    }
  },
  tableOperationMax: 10,
  btnConfig: {
    headerTitleConfigArr,
    tableAlertOptionRenderConfigArr,
    operationConfigRenderConfigArr,
  },
});
